package com.sbio.udiapp.ui;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.preference.DropDownPreference;
import androidx.preference.EditTextPreference;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceManager;

import com.sbio.udiapp.R;
import com.sbio.udiapp.di.ActivityManager;
import com.sbio.udiapp.util.DefaultSharedPreferencesUtil;

public class SettingsActivity extends AppCompatActivity implements SharedPreferences.OnSharedPreferenceChangeListener {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.settings_activity);
        if (savedInstanceState == null) {
            getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.settings, new SettingsFragment())
                    .commit();
        }
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayShowTitleEnabled(true);
        }


    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {

        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                break;
            default:
        }
        return true;
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        if (key.equals("useDept")) {
            // 当用户更改语言选项时，更新所选项的文本
//            binding.settings.get.setText(languagePreference.getEntry());
        }
    }

    public static class SettingsFragment extends PreferenceFragmentCompat {
        @Override
        public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
            setPreferencesFromResource(R.xml.root_preferences, rootKey);
            DropDownPreference useDeptPreference = findPreference(getString(R.string.use_dept));
//            EditTextPreference selectedText = findPreference("print_host");
            useDeptPreference.setOnPreferenceChangeListener((preference, newValue) -> {
//                selectedText.setText((String) newValue);
                preference.setTitle(getString(R.string.useDeptTitle) + ":" + newValue);
                return true;
            });
            useDeptPreference.setTitle(getString(R.string.useDeptTitle) + ":" + useDeptPreference.getValue());


        }
    }
}